import {useParams} from 'react-router-dom'
import {useEffect, useState} from 'react'
import Base64 from '@/utils/base64'

type keys<T> = keyof T

export const useQuery = <T extends Record<string, string>> () :T => {
  const params = useParams<{
    id: string
  }>()
  const [query, setQuery] = useState<T>({} as T)
  useEffect(() => {
    const base:Base64 = new Base64()
    const decode:string = base.decode( params.id! )
    const arr:string[] = decode.split("&")
    const obj:T = {} as T;
    for (const item of arr) {
      const list: [keys<T>, string] = item.split("=")
      obj[list[0]] = list[1] as T
    }
    setQuery(obj)
  }, [])
  return query
}
